package com.mdz.myapplication.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 孟德志
 * @create 2021-06-03-21:34
 */
public class XmlGen {
    /*
//         * 方法generateDocumentByMethod()通过调用方法构建xml文档：
//            1.使用DocumentHelper得到Document实例
//            Document document = DocumentHelper.createDocument();
//            2.创建Processing Instruction
//            document.addProcessingInstruction("xml-stylesheet", inMap);
//            3.创建元素Element
//            Element studentsElement = document.addElement("students");
//            4.为元素添加注释Comment
//            studentsElement.addComment("An Student Catalog");
//            5.为元素添加属性
//            studentsElement.addComment("An Student Catalog");
//            6.为元素添加文本值Text
//            ageElement.setText("18");
 */
    public Document generateDocumentByMethod() {

        Document document = DocumentHelper.createDocument();
        // ProcessingInstruction
        Map<String, String> inMap = new HashMap<String, String>();
        inMap.put("type", "text/xsl");
        inMap.put("href", "students.xsl");
        document.addProcessingInstruction("xml-stylesheet", inMap);
        // root element
        Element studentsElement = document.addElement("students");
        studentsElement.addComment("An Student Catalog");
        // son element
        Element stuElement = studentsElement.addElement("student");
        stuElement.addAttribute("sn", "01");
        Element nameElement = stuElement.addElement("name");
        nameElement.setText("sam");
        Element ageElement = stuElement.addElement("age");
        ageElement.setText("18");
        // son element
        Element anotherStuElement = studentsElement.addElement("student");
        anotherStuElement.addAttribute("sn", "02");
        Element anotherNameElement = anotherStuElement.addElement("name");
        anotherNameElement.setText("lin");
        Element anotherAgeElement = anotherStuElement.addElement("age");
        anotherAgeElement.setText("20");

        return document;
    }


    public Document genxml() {
        Document document = DocumentHelper.createDocument();
        // root element
        Element documentElement = document.addElement("Document");
//        documentElement.attribute("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"终端接口XML Schema-3.0.xsd\" SN=\"323232323\" Version=\"3.0\" License=\"00120-100290-002\"");

        //<Events>
        Element EventsElement = documentElement.addElement("Events");
        //<Event>
        Element EventElement = EventsElement.addElement("Event");
        EventElement.addAttribute("Name", "eventname");
        EventElement.addAttribute("MainAction", "mainaction");

        //<ActionMapping>
        Element ActionMappingElement = EventElement.addElement("ActionMapping");
        //<Action>
        Element ActionElement = ActionMappingElement.addElement("Action");
        ActionElement.addAttribute("Name", "mainaction");
        Element ActionDataElement = ActionElement.addElement("Action");
        ActionDataElement.setText("CorpOrderID");
        //<DataMaping>
        Element DataMapingElement = EventElement.addElement("DataMaping");

        Element MetaDataElement = DataMapingElement.addElement("Action");
        MetaDataElement.addAttribute("Name", "mainaction");
        MetaDataElement.addAttribute("Type", "String");
        //<DataField>
        Element DataFieldElement = EventElement.addElement("DataField");
        Element DataElement = DataFieldElement.addElement("DataField");
        DataElement.addAttribute("Code","code");
        DataElement.addAttribute("CorpOrderID","CorpOrderID");
        DataElement.addAttribute("Actor","Actor");
        DataElement.addAttribute("ActDate","ActDate");
        DataElement.addAttribute("ToCorpID","ToCorpID");
        return document;
    }


    /*
     * 方法generateDocumentByString()通过字符串转换直接构建xml文档，使用DocumentHelper.parseText()来实现.
     * document = DocumentHelper.parseText(text);
     */
    public Document generateDocumentByString(String text) {
        Document document = null;
        try {
            document = DocumentHelper.parseText(text);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }

    public String saveDocument(Document document, File outputXml) {
        try {
            // 美化格式
            OutputFormat format = OutputFormat.createPrettyPrint();
           /*// 缩减格式
           OutputFormat format = OutputFormat.createCompactFormat();*/
           /*// 指定XML编码
            format.setEncoding("GBK");*/
            format.setEncoding("UTF-8");
            XMLWriter output = new XMLWriter(new FileOutputStream(outputXml), format);
            output.write(document);
            output.close();
            return "成功";
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return e.getMessage();
        }
    }
}
